package com.sq.universe.system.service.impl;

import com.sq.universe.base.service.AuthorityService;
import com.sq.universe.base.service.impl.BaseServiceImpl;
import com.sq.universe.system.dao.UserDao;
import com.sq.universe.system.dao.UserDepartmentDao;
import com.sq.universe.system.entity.RoleMenuEntity;
import com.sq.universe.system.entity.UserDepartmentEntity;
import com.sq.universe.system.entity.UserEntity;
import com.sq.universe.system.entity.UserRoleEntity;
import com.sq.universe.system.service.UserRoleService;
import com.sq.universe.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 孙乾 2020-07-08 23:33:51
*/
@Service
public class UserServiceImpl extends BaseServiceImpl<UserEntity, UserDao> implements UserService {

    @Autowired
    private UserDao systemUserDao;

    @Autowired
    public UserDepartmentDao userDepartmentDao;

    @Autowired
    public AuthorityService authorityService;

    @Autowired
    public UserRoleService userRoleService;

    @Transactional
    @Override
    public List<String> getAuth(UserEntity user){
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setUrUserId(user.getUserId());
        List<UserRoleEntity> roles = authorityService.queryUserRole(userRoleEntity);
        List<String> authList = new ArrayList<>();
        for (UserRoleEntity role : roles) {
            RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
            roleMenuEntity.setRmRoleCode(role.getRole().getRoleCode());
            // 根据角色查询权限
            List<RoleMenuEntity> roleMenuList = authorityService.queryRoleMenu(roleMenuEntity);
            for (RoleMenuEntity roleMenu : roleMenuList) {
                authList.add(roleMenu.getRmMenuCode());
            }
        }
        //去重
        List<String> _authList = authList.stream().distinct().collect(Collectors.toList());
        return _authList;
    }

    @Transactional
    @Override
    public Integer insert(UserEntity userEntity){
        systemUserDao.insert(userEntity);
        if(!StringUtils.isEmpty(userEntity.getDepartmentCodes())){
            for(String departmentCode : userEntity.getDepartmentCodes().split(",") ){
                UserDepartmentEntity userDepartmentEntity = new UserDepartmentEntity();
                userDepartmentEntity.setUdUserId(userEntity.getUserId());
                userDepartmentEntity.setUdDepartmentCode(departmentCode);
                userDepartmentEntity.setUdCreateTime(new Date());
                userDepartmentEntity.setUdCreateUser(userEntity.getUserCreateUser());
                userDepartmentDao.insert(userDepartmentEntity);
            }
        }
        if(!StringUtils.isEmpty(userEntity.getUserRoles())){
            for(String roles : userEntity.getUserRoles().split(",") ){
                UserRoleEntity userRoleEntity = new UserRoleEntity();
                userRoleEntity.setUrUserId(userEntity.getUserId());
                userRoleEntity.setUrRoleCode(roles);
                userRoleEntity.setUrCreateTime(new Date());
                userRoleEntity.setUrCreateUser(userEntity.getUserCreateUser());
                userRoleService.insert(userRoleEntity);
            }
        }
        return userEntity.getUserId();
    }

    @Transactional
    @Override
    public Integer update(UserEntity userEntity){
        systemUserDao.update(userEntity);
        UserDepartmentEntity _userDepartmentEntity = new UserDepartmentEntity();
        _userDepartmentEntity.setUdUserId(userEntity.getUserId());
        userDepartmentDao.logicalDelete(_userDepartmentEntity);

        UserRoleEntity _userRoleEntity = new UserRoleEntity();
        _userRoleEntity.setUrUserId(userEntity.getUserId());
        userRoleService.logicalDelete(_userRoleEntity);
        if(!StringUtils.isEmpty(userEntity.getDepartmentCodes())){
            for(String departmentCode : userEntity.getDepartmentCodes().split(",") ){
                UserDepartmentEntity userDepartmentEntity = new UserDepartmentEntity();
                userDepartmentEntity.setUdUserId(userEntity.getUserId());
                userDepartmentEntity.setUdDepartmentCode(departmentCode);
                userDepartmentEntity.setUdCreateTime(new Date());
                userDepartmentEntity.setUdCreateUser(userEntity.getUserId());
                userDepartmentDao.insert(userDepartmentEntity);
            }
        }

        if(!StringUtils.isEmpty(userEntity.getUserRoles())){
            for(String roles : userEntity.getUserRoles().split(",") ){
                UserRoleEntity userRoleEntity = new UserRoleEntity();
                userRoleEntity.setUrUserId(userEntity.getUserId());
                userRoleEntity.setUrRoleCode(roles);
                userRoleEntity.setUrCreateTime(new Date());
                userRoleEntity.setUrCreateUser(userEntity.getUserCreateUser());
                userRoleService.insert(userRoleEntity);
            }
        }
        return userEntity.getUserId();
    }

}
